package ade.prime.model;

import java.util.Map;

import ade.prime.activity.R;
import ade.prime.activity.adapter.ResourceFetcher;
import ade.prime.service.AdeWebService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * @author		Schnell Michaël, Zanette Sébastien
 * @version		1.0
 */
public class AdeWebOptions implements Preferences, IntentConstants {
	
	protected static final String TAG = AdeWebOptions.class.getSimpleName() ;
	
	protected AdeWebOptions() {
		super() ;
	}// AdeWebOptions()
	
	public static int getTheme(Context context) {
		// Récupère les préférences de l'utilisateur
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		String preferenceKey = ResourceFetcher.getString(
				context, R.string.Preferences_theme) ; 
		String themeName = preferences.getString(preferenceKey, PREF_DEFAULT_THEME) ;
		int themeId = R.style.Theme_Sherlock_Light_ActionBarDark ;
		if (themeName.equals(PREF_THEME_DARK)) {
			themeId = R.style.Theme_Sherlock ;
		}// if
		return themeId ;
	}// getTheme(Activity)
	
	public static int getThemeAde(Context context) {
		// Récupère les préférences de l'utilisateur
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		String preferenceKey = ResourceFetcher.getString(
				context, R.string.Preferences_theme) ; 
		String themeName = preferences.getString(preferenceKey, PREF_DEFAULT_THEME) ;
		int themeId = R.style.Theme_Ade_Light ;
		if (themeName.equals(PREF_THEME_DARK)) {
			themeId = R.style.Theme_Ade_Dark ;
		}// if
		return themeId ;
	}// getTheme(Activity)
	
	public static int getBackgroundColor(int theme) {
		int themeId1 = R.style.Theme_Sherlock_Light_ActionBarDark ;
//		int themeId2 = R.style.Theme_Sherlock ;
		if (theme == themeId1) {
			return Color.WHITE ;
		}// if
		return Color.BLACK;
	}
	
	public static int getTextColor(int theme) {
//		int themeId1 = R.style.Theme_Sherlock_Light_ActionBarDark ;
		int themeId2 = R.style.Theme_Sherlock ;
		if (theme == themeId2) {
			return Color.WHITE ;
		}// if
		return Color.BLACK;
	}
	
	public static int getTextColorViewSchedule(int theme) {
//		int themeId1 = R.style.Theme_Sherlock_Light_ActionBarDark ;
		int themeId2 = R.style.Theme_Sherlock ;
		if (theme == themeId2) {
			return Color.LTGRAY ;
		}// if
		return Color.BLACK;
	}
	
	public static int getnHoursBetweenUpdates(Context context) {
		// Récupère les préférences de l'utilisateur
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		String preferenceKey = ResourceFetcher.getString(
				context, R.string.Preferences_sync_delay) ; 
		return Integer.parseInt(
				preferences.getString(preferenceKey, PREF_DEFAULT_SYNC_DELAY)) ;
	}// getnHoursBetweenUpdates(Context)
	
	public static boolean autoUpdateIsOn(Context context) {
		// Récupère les préférences de l'utilisateur
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		String preferenceKey = ResourceFetcher.getString(
				context, R.string.Preferences_sync) ; 
		return preferences.getBoolean(preferenceKey, PREF_SYNC_DEFAULT_VALUE) ;
	}// autoUpdateIsOn(Context)

	/**
	 * @param context
	 * @return
	 */
	public static boolean useAdeWebColors(Context context) {
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		String preferenceKey = ResourceFetcher.getString(
				context, R.string.Preferences_color_ADE) ; 
		return preferences.getBoolean(preferenceKey, PREF_COULEUR_ADE_DEFAULT_VALUE) ;
	}// useAdeWebColors(Context)
	
	public static boolean networkIsAvailable(Context context) {
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		String preferenceKey = ResourceFetcher.getString(
				context, R.string.Preferences_network_available) ; 
		return preferences.getBoolean(preferenceKey, true) ;
	}// networkIsAvailable(Context)
	
	public static void setNetworkAvailability(Context context,
			boolean networkIsAvailable) {
		SharedPreferences.Editor editor = 
				PreferenceManager.getDefaultSharedPreferences(context).edit() ;
		String preferenceKey = ResourceFetcher.getString(
				context, R.string.Preferences_network_available) ; 
		editor.putBoolean(preferenceKey, networkIsAvailable) ;
		editor.commit() ;
	}// networkIsAvailable(Context, boolean)
	
	protected static void printAllPreferences(Context context) {
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		Map<String, ?> params = preferences.getAll() ;
		for(String key : params.keySet()) {
			Log.d("AdeWebOptions", String.format( //$NON-NLS-1$
					"Key: '%s' - Value: '%s' of type %s",//$NON-NLS-1$
					key, params.get(key), 
					params.get(key).getClass().getSimpleName())) ;
		}// for
	}// printAllPreferences(Context)
	
	public static void registerChangeListener(Context context, 
			OnSharedPreferenceChangeListener listener) {
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		preferences.registerOnSharedPreferenceChangeListener(listener) ;
	}// registerChangeListener(Context, OnSharedPreferenceChangeListener)
	
	public static void unregisterChangeListener(Context context, 
			OnSharedPreferenceChangeListener listener) {
		SharedPreferences preferences = 
				PreferenceManager.getDefaultSharedPreferences(context) ;
		preferences.unregisterOnSharedPreferenceChangeListener(listener) ;
	}// unregisterChangeListener(Context, OnSharedPreferenceChangeListener)
	
	public static class PreferenceReplicator implements OnSharedPreferenceChangeListener {
		
		protected Context context ;

		/**
		 * @param context
		 */
		public PreferenceReplicator(Context context) {
			super();
			this.context = context;
		}// PreferenceReplicator(Context)

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			String preferenceKey ;
			boolean change = false ;
			Intent replicator = new Intent(this.context, AdeWebService.class) ;
			replicator.setAction(REPLICATE_PREFERENCES_ACTION) ;

//			Log.d(TAG, String.format("[onSharedPreferenceChanged] key: '%s'\n" + //$NON-NLS-1$
//					"\tColor: '%s' =?= %b\n" + //$NON-NLS-1$
//					"\tSyncr: '%s' =?= %b", //$NON-NLS-1$
//					key, 
//					ResourceFetcher.getString(this.context, R.string.Preferences_sync),
//					Boolean.valueOf(key.equals(ResourceFetcher.getString(this.context, R.string.Preferences_sync))), 
//					ResourceFetcher.getString(this.context, R.string.Preferences_sync_delay),
//					Boolean.valueOf(key.equals(ResourceFetcher.getString(this.context, R.string.Preferences_sync_delay))))) ; 
			
			// Auto-update
			preferenceKey = ResourceFetcher.getString(
					this.context, R.string.Preferences_sync) ;
			if (key.equals(preferenceKey)) {
				change = true ;
				replicator.putExtra(key, 
						sharedPreferences.getBoolean(key, false)) ;
			}// if
			// Number of hours between two updates
			preferenceKey = ResourceFetcher.getString(
					this.context, R.string.Preferences_sync_delay) ;
			if (key.equals(preferenceKey)) {
				change = true ;
				replicator.putExtra(key, 
						sharedPreferences.getString(key, PREF_DEFAULT_SYNC_DELAY)) ;
			}// if
			// Theme
			preferenceKey = ResourceFetcher.getString(
					this.context, R.string.Preferences_theme) ;
			if (key.equals(preferenceKey)) {
//				Log.d(TAG, "New theme: " + sharedPreferences.getString(key, PREF_DEFAULT_THEME)) ;
				change = true ;
				replicator.putExtra(key, 
						sharedPreferences.getString(key, PREF_DEFAULT_THEME)) ;
			}// if
			
			if (change) {
				this.context.startService(replicator) ;
			}// if
		}// onSharedPreferenceChanged(SharedPreferences, String)
		
	}

}// class AdeWebOptions